package org.jeecg.modules.chg.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.checkerframework.checker.units.qual.A;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.config.mybatis.TenantContext;
import org.jeecg.modules.arc.service.IArcAccountService;
import org.jeecg.modules.arc.service.IBasePeriodService;
import org.jeecg.modules.arc.vo.ArcAccountCusVO;
import org.jeecg.modules.bl.service.IBlExpensesService;
import org.jeecg.modules.bl.vo.BlReceiFeeCountVo;
import org.jeecg.modules.chg.entity.ChgFees;
import org.jeecg.modules.chg.entity.ChgSubFees;
import org.jeecg.modules.chg.service.IChgBillService;
import org.jeecg.modules.chg.service.IChgFeesService;
import org.jeecg.modules.chg.service.IChgPaymentsService;
import org.jeecg.modules.chg.service.IChgSubFeesService;
import org.jeecg.modules.chg.vo.ChgChargeFeeVo;
import org.jeecg.modules.chg.vo.ChgFeeCountVo;
import org.jeecg.modules.chg.vo.ChgFeesPage;
import org.jeecg.modules.chg.vo.ChgPaymentsCountVo;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
* @Description: 收费记录
* @Author: jeecg-boot
* @Date:   2022-02-21
* @Version: V1.0
*/
@Api(tags="收费记录")
@RestController
@RequestMapping("/chg/report")
@Slf4j
public class ChgFeeReportController {
   @Autowired
   private IChgFeesService chgFeesService;
   @Autowired
   private IChgPaymentsService chgPaymentsService;
   @Autowired
   private IBlExpensesService blExpensesService;


   /**
    * 分页列表查询
    *
    * @param chgFees
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "收费记录-分页列表查询")
   @ApiOperation(value="收费记录-分页列表查询", notes="收费记录-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(ChgFees chgFees,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<ChgFees> queryWrapper = QueryGenerator.initQueryWrapper(chgFees, req.getParameterMap());
       Page<ChgFees> page = new Page<ChgFees>(pageNo, pageSize);
       IPage<ChgFees> pageList = chgFeesService.page(page, queryWrapper);
       return Result.OK(pageList);
   }

    /**
     * 应收汇总
     *
     * {
     * 	"data": [{
     * 		"ctotal": "125箱",
     * 		"cname": "牛奶0",
     * 		"cprice": "56",
     * 		"riqi": "2021年08月11日",
     * 		"id": 100,
     * 		"dtotal": "1256箱",
     * 		"tp": "7000",
     * 		"ztotal": "589箱",
     * 		"cnum": "每箱12瓶",
     * 		"dId": "1"
     *        }],
     *   "total": 1, //total（总页数），count（总条数）
     * 	"count": 3
     *
     * }
     * @param financialMonth
     * @param req
     * @return
     */
    @AutoLog(value = "应收汇总")
    @ApiOperation(value="应收汇总", notes="应收汇总")
    @GetMapping(value = "/receivableFeeCount")
    public JSONObject receivableFeeCount(String financialMonth, HttpServletRequest req) {
        // 获取登录用户信息
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        System.out.println("当前租户："+sysUser.getRelTenantIds());
        TenantContext.setTenant(sysUser.getRelTenantIds());

        if(oConvertUtils.isEmpty(financialMonth)){
            financialMonth=DateUtils.getDate("yyyyMM");
        }
        List<BlReceiFeeCountVo> receiFeeCountVos = blExpensesService.receivableFeeCount(financialMonth);

        JSONObject jsonObject = new JSONObject();
        JSONArray rsArr= (JSONArray) JSONArray.toJSON(receiFeeCountVos);
        jsonObject.put("data",rsArr);
        System.out.println(jsonObject.toJSONString());
        return jsonObject;
    }

    /**
     * 收费汇总
     *

     * @param beginDate
     * @param endDate
     * @param req
     * @return
     */
    @AutoLog(value = "收费汇总")
    @ApiOperation(value="收费汇总", notes="收费汇总")
    @GetMapping(value = "/chargeFeeCount")
    public JSONObject chargeFeeCount(String beginDate,String endDate, HttpServletRequest req) {
        // 获取登录用户信息
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        System.out.println("当前租户："+sysUser.getRelTenantIds());
        TenantContext.setTenant(sysUser.getRelTenantIds());

        if(oConvertUtils.isEmpty(beginDate)){
            beginDate=DateUtils.getFirstMonth();
        }
        if(oConvertUtils.isEmpty(endDate)){
            try {
                endDate= DateUtils.formatAddDate(DateUtils.getDate("yyyy-MM-dd"),"yyyy-MM-dd",1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
            try {
                endDate= DateUtils.formatAddDate(endDate,"yyyy-MM-dd",1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        List<ChgFeeCountVo> chgFeeCountVos = chgFeesService.chargeFeeCount(beginDate,endDate);

        JSONObject jsonObject = new JSONObject();

        JSONArray rsArr= (JSONArray) JSONArray.toJSON(chgFeeCountVos);
        jsonObject.put("data",rsArr);
        System.out.println(jsonObject.toJSONString());
        return jsonObject;
    }

    /**
     * 收费实缴汇总-按天按收费员
     *

     * @param beginDate
     * @param endDate
     * @param req
     * @return
     */
    @AutoLog(value = "收费实缴汇总")
    @ApiOperation(value="收费实缴汇总", notes="收费实缴汇总")
    @GetMapping(value = "/chargePayCount")
    public JSONObject chargePayCount(String beginDate,String endDate, HttpServletRequest req) {
        // 获取登录用户信息
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        System.out.println("当前租户："+sysUser.getRelTenantIds());
        TenantContext.setTenant(sysUser.getRelTenantIds());

        if(oConvertUtils.isEmpty(beginDate)){
            beginDate=DateUtils.getFirstMonth();
        }
        if(oConvertUtils.isEmpty(endDate)){
            try {
                endDate= DateUtils.formatAddDate(DateUtils.getDate("yyyy-MM-dd"),"yyyy-MM-dd",1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
            try {
                endDate= DateUtils.formatAddDate(endDate,"yyyy-MM-dd",1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        List<ChgPaymentsCountVo> chgFeeCountVos = chgPaymentsService.chargePaymentsCount(beginDate,endDate);

        JSONObject jsonObject = new JSONObject();

        JSONArray rsArr= (JSONArray) JSONArray.toJSON(chgFeeCountVos);
        jsonObject.put("data",rsArr);
        System.out.println(jsonObject.toJSONString());
        return jsonObject;
    }

}
